import axios from 'axios'

const init={
    code_load:false,
    code_send:false,
    regist_load:false,
    msg:'',
    user:{},
    master:{},
    code:{}
}
//获取师傅信息
const MASTER_INFO_PRE = 'MASTER_INFO_PRE'
const MASTER_INFO_REV = 'MASTER_INFO_REV'
//发送验证码
const SEND_CODE_PRE = 'SEND_CODE_PRE'   
const SEND_CODE_REV = 'SEND_CODE_REV'
//注册用户
const REGIST_PRE = 'REGIST_PRE'
const REGIST_REV = 'REGIST_REV'
//消息管理
const MSG = 'MSG'
const CLEAR_MSG = 'CLEAR_MSG'

//定义redux
export function regist(state=init, action) {
    switch(action.type) {
        case MASTER_INFO_REV:
            return {...state, master: action.data}
        case SEND_CODE_PRE:
            return {...state, code_load: true}
        case SEND_CODE_REV:
            return {...state, code_load: false, code_send: true}
        case REGIST_PRE:
            return {...state, regist_load:true}
        case REGIST_REV:
            return {...state, regist_load: false, user:action.data}
        case MSG:
            return {...state, msg: action.msg}
        case CLEAR_MSG:
            return {...state, msg:''}
        default:
            return state
    }
}

//根据邀请码code获取师傅信息
export function masterInfo() {
    //获取code
    const inviteCode = ''
    if (inviteCode) {
        return dispatch=>{
            axios.get(`/master/${inviteCode}`).then(res=>{
                if (res.status == 200) {
                    const data = res.data
                    if (data.status == 0) {
                        return dispatch(masterInfoRev(data.data))
                    }
                    return dispatch(msg(data.msg))
                }
                return dispatch(msg('网络错误'))
            })
        }
    }
}

//发送验证码
export function sendCode(phone) {
    return dispatch=>{
        dispatch(sendCodePre())
        axios.post('/send_code', {phone}).then(res=>{
            if (res.status == 200) {
                const data = res.data
                if (data.status == 0) {
                    return dispatch(sendCodeRev())
                }
                return dispatch(msg(data.msg))
            }
            return dispatch(msg('网络错误'))
        })
    }
}

//注册
export function sendRegist({phone, code}) {
    return dispatch=>{
        dispatch(sendRegistPre())
        axios.post('/regist', {phone, code}).then(res=>{
            if (res.status == 200) {
                const data = res.data
                if (data.status == 0) {
                    return dispatch(sendRegistRev(data.data))
                }
                return dispatch(msg(data.msg))
            }
            return dispatch(msg('网络错误'))
        })
    }
}

//清理消息
export function clearMsg() {
    return {type: CLEAR_MSG}
}

//根据邀请码code获取师傅信息成功
function masterInfoRev(data) {
    return {
        type: MASTER_INFO_REV, data
    }
}

//发送验证码前
function sendCodePre() {
    return {type: SEND_CODE_PRE}
}
function sendCodeRev() {
    return {type: sendCodeRev}
}

//注册前
function sendRegistPre() {
    return {type: REGIST_PRE}
}
function sendRegistRev(data) {
    return {type: REGIST_REV, data}
}

//消息函数
function msg(msg) {
    return {
        type:msg
    }
}